﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace Assets.Script
{
    class SkinPanelControl
    {
        private SkinPanelView view;
        public SkinPanelModel model;
        List<GameObject> skinObjs = new List<GameObject>();

        bool isopen = false;
        float openTime = 2f;
        public SkinPanelControl(SkinPanelView skinPanelView)
        {
            this.view = skinPanelView;
            model = new SkinPanelModel("SkinPanelModelName");
            ModelManager.Ins.AddModel(model);
            BindEvent();
            MessageCenter.Ins.ListenMess<CurrencyData>(MessId.RefureCurrencyDataText, RefureCurrencyDataText);
            MessageCenter.Ins.ListenMess<int>(MessId.SkinIconRefureShow, SkinIconRefureShow);
            MessageCenter.Ins.ListenMess<string>(MessId.OpenTipsPanel, OpenTipsPanel);
            MainPanelModel models = ModelManager.Ins.GetModel(MainPanelModel.m_name) as MainPanelModel;
            MessageCenter.Ins.BuredMess(MessId.RefureCurrencyDataText, models.GetData());
            RefureNowBtnShow(model.GetNowSelectIndex());
            RefureSkinIcon(model.GetNowSelectIndex());
        }

        private void OpenTipsPanel(string obj)
        {
            isopen = true;
            openTime = 2f;
            view.tipsPanel.SetActive(true);
            view.tipsText.text = obj;
        }

        private void SkinIconRefureShow(int index)
        {
            for (int i = 0; i < skinObjs.Count; i++)
            {
                if (skinObjs[i].GetComponent<SkinIconSprite>().unid == index)
                {
                    skinObjs[i].GetComponent<SkinIconSprite>().RefuReRelever(true);
                    view.showNoeSkinImg.sprite = Resources.Load<Sprite>(skinObjs[i].GetComponent<SkinIconSprite>().GetMData().icon);
                }
                else
                {
                    skinObjs[i].GetComponent<SkinIconSprite>().RefuReRelever(false);
                }
            }
        }

        private void BindEvent()
        {
            view.closeBtn.onClick.AddListener(() =>
            {
                UIManager.Ins.CloseUI(UIName.SkinPanel);
            });
            view.addZuanshiBtn.onClick.AddListener(() =>
            {
                view.SatrtViedeo();
            });
            for (int i = 0; i < view.qualityBtns.Count; i++)
            {
                int index = i;
                view.qualityBtns[index].onClick.AddListener(() =>
                {
                    model.SetNowSelectIndex(index);
                    RefureNowBtnShow(index);
                    RefureSkinIcon(index);
                });
            }
        }

        private void RefureSkinIcon(int index)
        {
            for (int j = 0; j < GameDataSprite.Ins.allSkinDatas[index].Count; j++)
            {
                if (j > skinObjs.Count - 1)
                {
                    GameObject obj = GameObject.Instantiate(view.skinObj, view.skinTran);
                    obj.GetComponent<SkinIconSprite>().RefureData(GameDataSprite.Ins.allSkinDatas[index][j],j);
                    obj.SetActive(true);
                    skinObjs.Add(obj);
                }
                else
                {
                    skinObjs[j].GetComponent<SkinIconSprite>().RefureData(GameDataSprite.Ins.allSkinDatas[index][j],j);
                    skinObjs[j].SetActive(true);
                }
            }
            for (int i = 0; i < skinObjs.Count; i++)
            {
                if (i > GameDataSprite.Ins.allSkinDatas[index].Count - 1)
                {
                    skinObjs[i].SetActive(false);
                }
            }
        }

        private void RefureNowBtnShow(int index)
        {
            for (int i = 0; i < view.qualityBtns.Count; i++)
            {
                int indexNow = i;
                if (index == indexNow)
                {
                    view.qualityBtns[indexNow].transform.GetChild(1).gameObject.SetActive(true);
                    view.qualityBtns[indexNow].transform.GetChild(0).gameObject.SetActive(false);
                }
                else
                {
                    view.qualityBtns[indexNow].transform.GetChild(1).gameObject.SetActive(false);
                    view.qualityBtns[indexNow].transform.GetChild(0).gameObject.SetActive(true);
                }
            }
        }

        private void RefureCurrencyDataText(CurrencyData data)
        {
            view.zuanshiNumText.text = ShowCurrencyNum(data.zuanshi);
            view.goldNumText.text = ShowCurrencyNum(data.gold);
        }
        private string ShowCurrencyNum(int gold)
        {
            if (gold < 0)
                return "-" + ShowCurrencyNum(-gold);

            if (gold < 1000)
            {
                return gold.ToString();
            }
            else if (gold < 1_000_000)
            {
                return (gold / 1000.0).ToString("0.#") + "k";
            }
            else
            {
                return (gold / 1_000_000.0).ToString("0.#") + "m";
            }
        }

        internal void Update()
        {
            if (isopen)
            {
                openTime -= Time.deltaTime;
                if (openTime <= 0)
                {
                    view.tipsPanel.SetActive(false);
                }
            }
        }
    }
}
